package com.handinfo.android.ui.window;

import java.io.DataInputStream;
import java.io.IOException;
import java.util.concurrent.CopyOnWriteArrayList;

import android.graphics.Bitmap;
import android.widget.Toast;

import com.handinfo.android.DWGameManager;
import com.handinfo.android.core.graphics.DWFont;
import com.handinfo.android.core.object.Scene.SceneHandler;
import com.handinfo.android.game.Cell;
import com.handinfo.android.game.GameMessage;
import com.handinfo.android.game.Role;
import com.handinfo.android.game.item.Item;
import com.handinfo.android.game.item.ItemConstants;
import com.handinfo.android.ui.IUIWindows;
import com.handinfo.android.ui.UIWindows;
import com.handinfo.android.uicontrols.DWControlsManager;
import com.handinfo.android.uicontrols.DWListener;
import com.handinfo.android.uicontrols.controls.DWBackground;
import com.handinfo.android.uicontrols.controls.DWButton;
import com.handinfo.android.uicontrols.controls.DWFrame;
import com.handinfo.android.uicontrols.controls.DWGrid;
import com.handinfo.android.uicontrols.controls.DWGrids;
import com.handinfo.android.uicontrols.controls.DWImageBox;
import com.handinfo.android.uicontrols.controls.DWInputBox;
import com.handinfo.android.uicontrols.controls.DWListSubItem;
import com.handinfo.android.uicontrols.controls.DWListbox;
import com.handinfo.android.uicontrols.controls.DWTabControl;
import com.handinfo.android.uicontrols.controls.DWTabPage;
import com.handinfo.android.uicontrols.controls.DWTextbox;
import com.handinfo.android.uicontrols.controls.DWTitle;
import com.handinfo.android.utils.Tools;

/**
 * 
 * @author Xiaoyingying
 * 普通商店
 * 
 */
public class UINpcShopWindow implements IUIWindows
{

	public final static byte SHOP_STATS_BUY = 0; //买物品
	public final static byte SHOP_STATS_SALE = 1; //卖物品

	private byte m_shop_stats;
	private Bitmap img_close;
	private Bitmap img_grid;
	private Bitmap img_goodsBackground;
	private Bitmap img_page_before;
	private Bitmap img_page_next;
	private Bitmap img_button;
	private Bitmap img_anxia;
	private Bitmap tabSelectImg;// 选项卡选中的图标
	private Bitmap tabUnSelectImg;// 先选卡未被选中的图标
	private Bitmap img_title;
	private Bitmap img_jinbi;

	public void init()
	{
		img_close = UIWindows.createImage("/img/newui/fanhuianniu_1.gnp");
		img_grid = UIWindows.createImage("/img/newui/wupinlan.gnp");
		img_goodsBackground = UIWindows.createImage("/img/newui/zhujiemianltk_1.gnp");
		img_page_before = UIWindows.createImage("/img/newui/haoyoufyan_2.gnp");
		img_page_next = UIWindows.createImage("/img/newui/haoyoufyan_1.gnp");
		img_button = UIWindows.createImage("/img/newui/anniu_2.gnp");
		img_anxia = UIWindows.createImage("/img/newui/anniu_2ax.gnp");
		tabSelectImg = UIWindows.createImage("/img/newui/biaoqianye_2.gnp");
		tabUnSelectImg = UIWindows.createImage("/img/newui/biaoqianye_1.gnp");
		img_title = UIWindows.createImage("/img/newui/wupingchushou_1.gnp");
		img_jinbi = UIWindows.createImage("/img/newui/jinbi_1.gnp");
	}

	public void open(byte index)
	{
		selectIndex = -1;
		m_shop_stats = index;
		if (((SceneHandler) DWGameManager.getInstance().getGameHandler()).m_scene != null)
		{
			if (((SceneHandler) DWGameManager.getInstance().getGameHandler()).m_scene.m_dwDMNPCTaskList != null)
			{
				DWControlsManager.getInstance().removeControl(
						((SceneHandler) DWGameManager.getInstance().getGameHandler()).m_scene.m_dwDMNPCTaskList);
			}
		}
		initFrame();
		switch (m_shop_stats) {
			case SHOP_STATS_BUY:
				addSubItem();
				break;
			case SHOP_STATS_SALE:
				m_shop_sellType = Role.CURRENCY_TYPE_GOLD;
				addTablepage();
				break;
		}

		DWControlsManager.getInstance().addControl(m_npc_shop);
	}

	public void close(byte index)
	{
		if (DWControlsManager.getInstance().contains(m_npc_shop))
		{
			DWControlsManager.getInstance().removeControl(m_npc_shop);
			m_npc_shop = null;
		}
		if (DWControlsManager.getInstance().contains(m_npc_goodsMsg))
		{
			DWControlsManager.getInstance().removeControl(m_npc_goodsMsg);
			m_npc_goodsMsg = null;
		}
		m_shop_item = null;
	}

	public void dispose()
	{
	}

	public void logic()
	{
	}

	public DWFrame m_npc_shop; //商店窗口
	private DWTitle m_npc_shoptitle;
	private DWImageBox m_npc_close; //关闭窗口
	private DWBackground m_npc_shopBackground;
	private DWListbox m_npc_goodslist;
	private DWTextbox m_npc_money;

	private void initFrame()
	{
		if (DWControlsManager.getInstance().contains(m_npc_shop))
		{
			DWControlsManager.getInstance().removeControl(m_npc_shop);
			m_npc_shop = null;
		}

		m_npc_shop = new DWFrame(DWFrame.FRAME_TYPE_HALF_LEFT);
		m_npc_shop.setClickClose(true);

		m_npc_shoptitle = new DWTitle(m_shop_title, m_npc_shop);
		if (m_shop_stats == SHOP_STATS_SALE)
			m_npc_shoptitle.setBackground(img_title, false);

		m_npc_shop.addControl(m_npc_shoptitle);
		m_npc_shop.setClickClose(false);

		m_npc_close = new DWImageBox(img_close);
		m_npc_close.setNearAnchor(m_npc_shoptitle, Tools.RIGHT_VCENTER, Tools.RIGHT_VCENTER);
		m_npc_close.setTouchZoomIn(UIFixWindow.TOUCHZOOM, UIFixWindow.TOUCHZOOM);
		m_npc_close.addListener(new DWListener() {
			public void OnDoubleClick()
			{
			}

			public void OnClick()
			{
				close((byte) 0);
			}
		});
		m_npc_shop.addControl(m_npc_close);

		m_npc_money = new DWTextbox("");
		m_npc_money.setNearAnchor(m_npc_shop, Tools.LEFT_BOTTOM, Tools.LEFT_BOTTOM, 20, -10);
		m_npc_shop.addControl(m_npc_money);
		
		m_npc_shopBackground = new DWBackground(m_npc_shop.getShowWidth() - 20, m_npc_shop.getShowHeight() - 50 -img_jinbi.getHeight());
//				.getShowHeight() - 50 -(img_jinbi.getHeight()>m_npc_money.getShowHeight()?img_jinbi.getHeight():m_npc_money.getShowHeight()));
		m_npc_shopBackground.setNearAnchor(m_npc_shop, Tools.HCENTER_VCENTER, Tools.HCENTER_VCENTER);
		m_npc_shop.addControl(m_npc_shopBackground);

		
	}

	private DWListSubItem[] m_npc_subList;
	private DWBackground[] m_npc_itemackground;
	private DWGrid[] m_npc_itemImg;
	private DWTextbox[] m_npc_name;
	private DWTextbox[] m_npc_price;

	/**
	 * 购买界面中添加列表中内容
	 */
	private void addSubItem()
	{
		m_npc_goodslist = new DWListbox(m_npc_shopBackground.getShowWidth() - 10,
				m_npc_shopBackground.getShowHeight() - 10);
		m_npc_goodslist.setLineSpacing(10);
		m_npc_goodslist.setShowBackgroundRect(false);
		m_npc_goodslist.setNearAnchor(m_npc_shopBackground, Tools.HCENTER_VCENTER, Tools.HCENTER_VCENTER);
		setOwnMoney();
		addGoodsItem();
		m_npc_shop.addControl(m_npc_goodslist);
	}

	/**
	 * 设置自己拥有货币数
	 */

	private DWImageBox m_ownMoney;

	private void setOwnMoney()
	{
		long price = getOwnMoney(m_shop_sellType);
		m_npc_money.setText("<mark c=FFF8DC>"
				+ UIBag.splitMoney(price) +(m_shop_sellType == Role.CURRENCY_TYPE_GOLD ? "": getType())
				+ "</mark>");
		if(m_shop_sellType==Role.CURRENCY_TYPE_GOLD)
		{
			if(m_ownMoney==null)
			{
				m_ownMoney=new DWImageBox(img_jinbi);
			}
			if(!m_npc_shop.contains(m_ownMoney))
			{
				m_npc_shop.addControl(m_ownMoney);
			}
			m_ownMoney.setNearAnchor(m_npc_money, Tools.LEFT_VCENTER, Tools.RIGHT_VCENTER,5,0);
		}
	}

	/**
	 * 获取自己对应货币类型的值
	 * @param type
	 * @return
	 */
	private long getOwnMoney(byte type)
	{
		long price = 0;
		Role role = DWGameManager.getInstance().m_role;
		switch (type) {
			case Role.CURRENCY_TYPE_GOLD:
				price = role.m_gold;
				break;
			case Role.CURRENCY_TYPE_BANGGONG:
				price = role.m_banggong;
				break;
			case Role.CURRENCY_TYPE_RONGYU:
				price = role.m_rongyu;
				break;
			case Role.CURRENCY_TYPE_ZHANGONG:
				price = role.m_zhangong;
				break;
		}

		return price;
	}

	private int selectIndex = -1;

	private void addGoodsItem()
	{
		selectIndex = -1;

		if (m_shop_item == null || m_shop_item.length < 1)
			return;

		m_npc_goodslist.removeAllSubItem();
		int length = m_shop_item.length;

		m_npc_subList = new DWListSubItem[(length + 1) / 2];
		m_npc_itemackground = new DWBackground[length];
		m_npc_itemImg = new DWGrid[length];
		m_npc_name = new DWTextbox[length];
		m_npc_price = new DWTextbox[length];
		for (int i = 0; i < length; i++)
		{

			final int num = i;
			m_npc_itemackground[i] = new DWBackground(img_goodsBackground, (m_npc_goodslist.getShowWidth() - 15) / 2,
					(m_npc_goodslist.getShowHeight() - 20) / 4);

			if (i % 2 == 0) {
				m_npc_subList[i / 2] = new DWListSubItem();
				m_npc_itemackground[i].setNearAnchor(m_npc_subList[i / 2], Tools.LEFT_TOP, Tools.LEFT_TOP);
			}
			else {
				m_npc_itemackground[i].setNearAnchor(m_npc_itemackground[i - 1], Tools.LEFT_VCENTER,
						Tools.RIGHT_VCENTER, 5, 0);
			}

			m_npc_subList[i / 2].addControls(m_npc_itemackground[i]);

			m_npc_itemImg[i] = new DWGrid(img_grid);
			m_npc_itemImg[i].setDarg(true);
			m_npc_itemImg[i].addObject(m_shop_item[i]);
			m_npc_itemImg[i].setNearAnchor(m_npc_itemackground[i], Tools.LEFT_VCENTER, Tools.LEFT_VCENTER, 5,
					0);
			m_npc_itemImg[i].addListener(new DWListener() {
				public void OnDoubleClick()
				{
				}

				public void OnClick()
				{
					if (selectIndex > -1)
					{
						m_npc_itemImg[selectIndex].setSelect(false);
					}
					selectIndex = num;
					m_npc_itemImg[selectIndex].setSelect(true);
					showItemMessage(m_shop_item[num]);
				}
			});
			m_npc_subList[i / 2].addControls(m_npc_itemImg[i]);

			m_npc_name[i] = new DWTextbox("<mark c="
					+ UIBag.getNameColor(m_shop_item[i].m_quality) + ">"
					+ m_shop_item[i].m_name + "</mark>");
			m_npc_name[i].setFont(DWFont.getFont(15));
			m_npc_name[i].setNearAnchor(m_npc_itemImg[i], Tools.LEFT_BOTTOM, Tools.RIGHT_VCENTER, 5, -10);
			m_npc_subList[i / 2].addControls(m_npc_name[i]);

			m_npc_price[i] = new DWTextbox("" + getItemPrice(m_shop_item[i], m_shop_sellType) + getType());
			m_npc_price[i].setFont(DWFont.getFont(15));
			m_npc_price[i].setNearAnchor(m_npc_itemImg[i], Tools.LEFT_TOP, Tools.RIGHT_VCENTER, 5, 10);
			m_npc_subList[i / 2].addControls(m_npc_price[i]);
			if (i % 2 == 0)
				m_npc_goodslist.addSubItem(m_npc_subList[i / 2]);
		}
	}

	private String[] m_npc_pageName = { "全部", "装备", "消耗", "宝石", "其他" };
	private DWTabControl m_npc_tabcontrol;
	private DWTabPage[] m_npc_tabpage = new DWTabPage[m_npc_pageName.length];

	/**
	 * 出售界面中添加背包页
	 */
	private void addTablepage()
	{
		m_npc_tabcontrol = new DWTabControl(tabUnSelectImg, tabSelectImg, m_npc_shopBackground.getShowWidth() - 20,
				m_npc_shopBackground.getShowHeight() - 10);
		m_npc_tabcontrol.setNearAnchor(m_npc_shopBackground, Tools.HCENTER_VCENTER, Tools.HCENTER_VCENTER);
		for (int i = 0; i < m_npc_pageName.length; i++) {
			m_npc_tabpage[i] = new DWTabPage(); // 建立标签页
			m_npc_tabpage[i].setName(m_npc_pageName[i]);// 设置标签页标		
			addGrids(i);
		}
		refreshBag();
		m_npc_tabcontrol.addListener(new DWListener() {
			public void OnDoubleClick()
			{
			}

			public void OnClick()
			{
				refreshBag();
			}
		});
		m_npc_tabcontrol.addTabPages(m_npc_tabpage);
		m_npc_shop.addControl(m_npc_tabcontrol);
	}

	/**
	 * 在标签页中添加背包格
	 */

	private DWGrids m_npc_grids[] = new DWGrids[m_npc_pageName.length];
	private int length;

	private void addGrids(final int num)
	{
		if (DWGameManager.getInstance().m_role.m_column_bag != null)
		{
			length = DWGameManager.getInstance().m_role.m_column_bag.length;
			m_npc_grids[num] = new DWGrids(img_grid, length, 5, 4);
			int gridsX = (m_npc_tabcontrol.getShowWidth() - img_grid.getWidth() * 5 - 10) / 5;
			int gridsY = (m_npc_tabcontrol.getShowHeight() - img_grid.getWidth() * 4 - 10 - 37) / 4;

			m_npc_grids[num].setInterspace(gridsX, gridsY);

			m_npc_grids[num].addListener(new DWListener() {
				public void OnDoubleClick()
				{
				}

				public void OnClick()
				{
					if (m_npc_grids[num].getTouchGrid().m_data != null
							&& ((Cell) m_npc_grids[num].getTouchGrid().m_data).m_item != null)
					{
						if (DWGameManager.getInstance().getSendMessage().isNetLocked(GameMessage.锁_出售))
							return;

						showItemMessage(m_npc_grids[num].getTouchGrid().m_data);
					}
				}
			});
			m_npc_grids[num].setNearAnchor(m_npc_tabpage[num], Tools.LEFT_TOP,
					Tools.LEFT_TOP); // 设置控件在标签页中的位置
			m_npc_tabpage[num].addControls(m_npc_grids[num]); // 把控件添加到标签页中
		}
	}

	/**
	 * 更新当前页中的物品
	 */
	private void refreshBag()
	{
		int index = m_npc_tabcontrol.getPageIndex();
		setGrids(index, getBagDate(index));
	}

	/**
	 * 在格子中添加数据
	 * @param num
	 */
	private void setGrids(int num, Cell[] cell)
	{
		for (int i = 0; i < length; i++)
		{
			m_npc_grids[num].m_grids.get(i).m_data = null;
		}

		if (cell != null && cell.length > 0)
		{
			for (int i = 0; i < cell.length; i++)
			{
				m_npc_grids[num].m_grids.get(i).addObject(cell[i]);
			}
		}
	}

	/**
	 * 获取背包数据
	 * 
	 */

	private Cell[] getBagDate(int columnType)
	{
		Cell[] cell = null;
		if (DWGameManager.getInstance().m_role != null)
		{
			if (DWGameManager.getInstance().m_role.m_column_bag != null)
			{
				Cell[] m_npc_bag = DWGameManager.getInstance().m_role.m_column_bag;

				if (columnType == Cell.INDEX_ROLEBAG_ALL)
					return m_npc_bag;

				int num = m_npc_bag.length;
				if (m_npc_bag != null && num > 0) {
					CopyOnWriteArrayList<Cell> array = new CopyOnWriteArrayList<Cell>();
					for (Cell c : m_npc_bag) {
						if (c.m_item != null && c.m_item.m_type == columnType) {
							array.add(c);
						}
					}
					int size = array.size();
					cell = new Cell[size];
					for (int i = 0; i < size; i++) {
						Cell m = array.get(i);
						cell[i] = m;
						Tools.debugPrintln("m.name=" + m.m_item.m_name);
					}
					array = null;
				}
			}
		}
		return cell;
	}

	private DWFrame m_npc_goodsMsg; //商品信息窗口
	private DWTitle m_msg_title;
	private DWImageBox m_msg_close;
	private DWBackground m_msg_winBackground;
	private DWBackground m_msg_itemMsgBackground;
	private DWListbox m_msg_listbox;
	private DWTextbox m_msg_numLable;
	private DWImageBox m_msg_plusNum;
	private DWImageBox m_msg_addNum;
	private DWInputBox m_msg_num;
	private DWTextbox m_msg_itemPrice;
	private DWButton m_msg_button;
	String title = "";
	String m_buy_num = "1";
	private Item item;

	/**
	 * 显示选中的信息
	 */
	private void showItemMessage(final Object obj)
	{
		if (obj == null)
			return;

		item = null;

		if (obj instanceof Item)
			item = (Item) obj;
		else if (obj instanceof Cell)
			item = ((Cell) obj).m_item;
		else
			return;

		if (DWControlsManager.getInstance().contains(m_npc_goodsMsg))
		{
			DWControlsManager.getInstance().removeControl(m_npc_goodsMsg);
			m_npc_goodsMsg = null;
		}

		m_npc_goodsMsg = new DWFrame(DWFrame.FRAME_TYPE_HALF_RIGHT);
		m_npc_goodsMsg.setClickClose(true);
		m_npc_goodsMsg.setTouchZoomIn(0, 0);

		m_buy_num = "1";
		title = "";
		switch (m_shop_stats) {
			case SHOP_STATS_BUY:
				title = "购买";
				break;
			case SHOP_STATS_SALE:
				title = "出售";
				break;
		}

		m_msg_title = new DWTitle(title, m_npc_goodsMsg);
		m_npc_goodsMsg.addControl(m_msg_title);

		m_msg_close = new DWImageBox(img_close);
		m_msg_close.setNearAnchor(m_msg_title, Tools.RIGHT_VCENTER, Tools.RIGHT_VCENTER);
		m_msg_close.setTouchZoomIn(UIFixWindow.TOUCHZOOM, UIFixWindow.TOUCHZOOM);
		m_msg_close.addListener(new DWListener() {
			public void OnDoubleClick()
			{
			}

			public void OnClick()
			{
				DWControlsManager.getInstance().removeControl(m_npc_goodsMsg);
				m_npc_goodsMsg = null;
			}
		});
		m_npc_goodsMsg.addControl(m_msg_close);

		m_msg_winBackground = new DWBackground(m_npc_goodsMsg.getShowWidth() - 20, m_npc_goodsMsg
				.getShowHeight() - 40);
		m_msg_winBackground.setNearAnchor(m_msg_title, Tools.HCENTER_TOP, Tools.HCENTER_BOTTOM);
		m_npc_goodsMsg.addControl(m_msg_winBackground);

		if (m_shop_stats == SHOP_STATS_SALE && item.m_num == 1)
		{
			m_msg_itemMsgBackground = new DWBackground(img_goodsBackground, m_msg_winBackground.getShowWidth() - 20,
					m_msg_winBackground.getShowHeight() * 4 / 5);
		}
		else
			m_msg_itemMsgBackground = new DWBackground(img_goodsBackground, m_msg_winBackground.getShowWidth() - 20,
					m_msg_winBackground.getShowHeight() * 2 / 3);
		m_msg_itemMsgBackground.setNearAnchor(m_msg_winBackground, Tools.LEFT_TOP, Tools.LEFT_TOP, 10, 10);
		m_npc_goodsMsg.addControl(m_msg_itemMsgBackground);

		m_msg_listbox = new DWListbox(m_msg_itemMsgBackground.getShowWidth() - 20, m_msg_itemMsgBackground
				.getShowHeight() - 20);
		m_msg_listbox.setShowBackgroundRect(false);
		m_msg_listbox.setNearAnchor(m_msg_itemMsgBackground, Tools.HCENTER_VCENTER, Tools.HCENTER_VCENTER);
		m_npc_goodsMsg.addControl(m_msg_listbox);

		DWListSubItem subItem = new DWListSubItem();

		UIWindows.getInstance().m_bag.getItemWindow(subItem, 0,0, m_msg_listbox.getShowWidth(), item,m_shop_sellType);

		m_msg_listbox.addSubItem(subItem);

		if (m_shop_stats != SHOP_STATS_SALE || item.m_num > 1)
		{

			m_msg_numLable = new DWTextbox(title + "数量：");
			m_msg_numLable.setNearAnchor(m_msg_itemMsgBackground, Tools.LEFT_TOP, Tools.LEFT_BOTTOM, 20, 30);
			m_npc_goodsMsg.addControl(m_msg_numLable);

			m_msg_plusNum = new DWImageBox(img_page_before);
			m_msg_plusNum.addListener(new DWListener() {

				public void OnDoubleClick()
				{
				}

				public void OnClick()
				{
					if (!isNumber(m_msg_num.getText()))
						return;

					int num = Integer.valueOf(m_msg_num.getText());

					if (num > 1)
						num--;
					else
					{
						switch (m_shop_stats) {
							case SHOP_STATS_BUY:
								num = 999;
								break;

							case SHOP_STATS_SALE:
								if (item.m_num == 1)
									return;
								else
									num = item.m_num;
								break;
						}
					}
					m_buy_num = "" + num;
					m_msg_num.setText(m_buy_num);
					setItemPrice(item, num);

				}
			});
			m_msg_plusNum.setNearAnchor(m_msg_numLable, Tools.LEFT_VCENTER, Tools.RIGHT_VCENTER, 20, 0);
			m_npc_goodsMsg.addControl(m_msg_plusNum);

			m_msg_num = new DWInputBox(80, 40, m_npc_goodsMsg);
			m_msg_num.setText(m_buy_num);
			m_msg_num.addListener(new DWListener() {
				public void OnDoubleClick()
				{
				}

				public void OnClick()
				{
					if (!m_buy_num.equals(m_msg_num.getText()))
					{
						if (isNumber(m_msg_num.getText()))
						{
							int num = Integer.valueOf(m_msg_num.getText());

							if (num < 1)
								num = 1;
							else
							{
								switch (m_shop_stats) {
									case SHOP_STATS_BUY:
										if (num > 999)
											num = 999;
										break;
									case SHOP_STATS_SALE:
										if (num > item.m_num)
											num = item.m_num;
										break;
								}
							}
							m_buy_num = "" + num;
							m_msg_num.setText(m_buy_num);
							setItemPrice(item, num);
						}
						else
						{
							m_msg_num.setText(m_buy_num);
						}
					}
				}
			});
			m_msg_num.setNearAnchor(m_msg_plusNum, Tools.LEFT_VCENTER, Tools.RIGHT_VCENTER, 20, 0);
			m_npc_goodsMsg.addControl(m_msg_num);

			m_msg_addNum = new DWImageBox(img_page_next);
			m_msg_addNum.addListener(new DWListener() {
				public void OnDoubleClick()
				{
				}

				public void OnClick()
				{
					if (!isNumber(m_msg_num.getText()))
						return;

					int num = Integer.valueOf(m_msg_num.getText());
					int maxNum = (m_shop_stats == SHOP_STATS_BUY) ? 999 : item.m_num;
					if (num < maxNum)
						num++;
					else
						num = 1;
					m_buy_num = "" + num;
					m_msg_num.setText(m_buy_num);
					setItemPrice(item, num);
				}
			});
			m_msg_addNum.setNearAnchor(m_msg_num, Tools.LEFT_VCENTER, Tools.RIGHT_VCENTER, 20, 0);
			m_npc_goodsMsg.addControl(m_msg_addNum);
		}

		m_msg_itemPrice = new DWTextbox("");		
		m_msg_itemPrice.setNearAnchor(m_msg_winBackground, Tools.LEFT_BOTTOM, Tools.LEFT_BOTTOM, 10, -10);
		m_npc_goodsMsg.addControl(m_msg_itemPrice);
		setItemPrice(item, 1);

		if (m_shop_stats != SHOP_STATS_SALE || item.isOperate(ItemConstants.可出售))
		{
			m_msg_button = new DWButton(title, img_button);
			m_msg_button.setDownImage(img_anxia);
			m_msg_button.addListener(new DWListener() {
				public void OnDoubleClick()
				{
				}

				public void OnClick()
				{
					DWControlsManager.getInstance().removeControl(m_npc_goodsMsg);
					m_npc_goodsMsg = null;
					int num;
					//
//					if(obj instanceof Cell &&((Cell) obj).m_item.m_num>1)
//					{
//						if (!isNumber(m_msg_num.getText()))
//							return;
					//
//						num = Integer.valueOf(m_msg_num.getText());
//					}	
					if (m_shop_stats == SHOP_STATS_SALE && ((Cell) obj).m_item.m_num == 1)
						num = 1;
					else
					{
						if (!isNumber(m_msg_num.getText()))
						{
							DWGameManager.getInstance().addSystemInfo(DWGameManager.SYSTEM_INFO_TYPE_B, "请输入正确的数量~");
							return;
						}

						num = Integer.valueOf(m_msg_num.getText());
					}

					switch (m_shop_stats) {
						case SHOP_STATS_BUY:
							DWGameManager.getInstance().getSendMessage().sendShopBuyItem(m_shop_sellType, item.m_key,
									num);
							break;
						case SHOP_STATS_SALE:
							DWGameManager.getInstance().getSendMessage().sendNpcShopSail(
									(byte) ((Cell) obj).m_column_type,
									((Cell) obj).m_index, num);
							break;
					}
				}
			});
			m_msg_button.setNearAnchor(m_msg_winBackground, Tools.RIGHT_BOTTOM, Tools.RIGHT_BOTTOM, -20, -10);
			m_npc_goodsMsg.addControl(m_msg_button);
		}

		DWControlsManager.getInstance().addControl(m_npc_goodsMsg);
	}

	/**
	 * 设置物品出售或购买的总价
	 * @param item
	 * @param num
	 */
	private DWImageBox m_itemMoney;
	private void setItemPrice(Item item, int num)
	{
		int price = getItemPrice(item, m_shop_sellType);
		m_msg_itemPrice.setText(title + "总价：<mark c=00BBFF>"
				+  UIBag.splitMoney(price * num)
				+ (m_shop_sellType == Role.CURRENCY_TYPE_GOLD ?"":getType()) + "</mark>");
		
		if(m_shop_sellType==Role.CURRENCY_TYPE_GOLD)
		{
			if(m_itemMoney==null)
			{
				m_itemMoney=new DWImageBox(img_jinbi);
			}
			if(!m_npc_goodsMsg.contains(m_itemMoney))
			{
				m_npc_goodsMsg.addControl(m_itemMoney);
			}
			m_itemMoney.setNearAnchor(m_msg_itemPrice, Tools.LEFT_VCENTER, Tools.RIGHT_VCENTER,5,0);
		}
	}

	/**
	 * 获取物品的对应售价
	 * @param item
	 * @param type
	 * @return
	 */
	static public int getItemPrice(Item item, byte type)
	{
		int price = 0;
		switch (type) {
			case Role.CURRENCY_TYPE_GOLD:
				price = item.m_goldPrice;
				break;
			case Role.CURRENCY_TYPE_BANGGONG:
				price = item.m_banggongPrice;
				break;
			case Role.CURRENCY_TYPE_RONGYU:
				break;
			case Role.CURRENCY_TYPE_ZHANGONG:
				price = item.m_zhangongPrice;
				break;
		}
		return price;
	}
	
	private String getType()
	{
		String type = "";
		switch (m_shop_sellType) {
			case Role.CURRENCY_TYPE_GOLD:
				type = "金";
				break;
			case Role.CURRENCY_TYPE_BANGGONG:
				type = "贡献值";
				break;
			case Role.CURRENCY_TYPE_RONGYU:
				type = "荣誉";
				break;
			case Role.CURRENCY_TYPE_ZHANGONG:
				type = "战功";
				break;
		}
		return type;
	}

	public static boolean isNumber(String s)
	{
		if ((s != null) && (!s.equals("")))
		{
			return s.matches("^[0-9]*$");
		}
		else
			return false;
	}

	private String m_shop_title;
	private Item[] m_shop_item;
	private byte m_shop_sellType;

	/**
	 * 打开商店界面
	 * @param dis
	 */
	public void recvNpcShopList(DataInputStream dis)
	{
		Tools.debugPrintln("recvNpcShopList ");
		try {
			byte state = dis.readByte();
			if (state == 0) {
				return;
			}
			String shopname = dis.readUTF();//商店名字
			String shopdescrip = dis.readUTF();//商店描述
			Tools.debugPrintln("shopdescrip=" + shopdescrip);
			byte sellType = dis.readByte();//出售价格类型
			int size = dis.readInt();
			Tools.debugPrintln("size=" + size);

			Item[] items = new Item[size];
			for (int i = 0; i < size; i++) {
				items[i] = Item.recvItemFormServer(dis);
			}
			// 创建界面与界面显示数据
			m_shop_title = shopname;
			m_shop_item = items;
			m_shop_sellType = sellType;
			items = null;
			open(SHOP_STATS_BUY);
		}
		catch (Exception e) {
			Tools.debug(e);
		}
	}

	/**
	 * 购买道具是否成功
	 * @param dis
	 */
	public void recvShopBuyItem(DataInputStream dis)
	{
		Tools.debugPrintln("recvShopBuyItem ");
		try {
			byte state = dis.readByte();
			if (state == 0) {
				DWGameManager.getInstance().showToast("购买失败!", Toast.LENGTH_SHORT);
			}
			else if (state == 1) {
				DWGameManager.getInstance().showToast("购买成功", Toast.LENGTH_SHORT);
				setOwnMoney();
			}
			else if (state == 2) {
				DWGameManager.getInstance().showToast("失败，道具不存在!", Toast.LENGTH_SHORT);
			}
			else if (state == 3) {
				DWGameManager.getInstance().showToast("失败，金钱不足!", Toast.LENGTH_SHORT);
			}
			else if (state == 4) {
				DWGameManager.getInstance().showToast("失败，背包已满!", Toast.LENGTH_SHORT);
			}
			else if (state == 5) {
				DWGameManager.getInstance().showToast("失败，背包已满，请扩充背包!", Toast.LENGTH_SHORT);
			}
		}
		catch (Exception e) {
			Tools.debug(e);
		}
	}

	/**
	 * 进入卖商店
	 * @param dis
	 * @throws IOException
	 */
	public void recvOpenSailShop(DataInputStream dis)
	{
		try {
			byte state = dis.readByte();
			if (state == 0) {
				DWGameManager.getInstance().addSystemInfo(DWGameManager.SYSTEM_INFO_TYPE_B, "打开商店失败~");
				return;
			}
			m_shop_sellType = Role.CURRENCY_TYPE_GOLD;
			open(SHOP_STATS_SALE);
			setOwnMoney();
		}
		catch (IOException e) {
			Tools.debugPrintln("开启贩卖商店出错");
		}
	}

	/**
	 * 物品出售
	 * @param dis
	 * @throws IOException
	 */
	public void recvNpcshopSail(DataInputStream dis) throws IOException
	{
		DWGameManager.getInstance().getSendMessage().unlockNetwork(GameMessage.锁_出售);
		byte state = dis.readByte();
		switch (state) {
			case 0:
				DWGameManager.getInstance().addSystemInfo(DWGameManager.SYSTEM_INFO_TYPE_B, "出售失败");
				break;
			case 1:
				// 贩卖成功
				DWGameManager.getInstance().addSystemInfo(DWGameManager.SYSTEM_INFO_TYPE_B, "出售成功");
				refreshBag();
				setOwnMoney();
				break;
			default:
				DWGameManager.getInstance().addSystemInfo(DWGameManager.SYSTEM_INFO_TYPE_B, "道具不可出售");
				break;
		}
	}

}
